package cn.web.management.service.general;

import cn.web.management.dao.impl.FindAllImpl;
import cn.web.management.dao.inface.FindAllDao;
import cn.web.management.domain.DepartmentInform;
import cn.web.management.domain.GroupInform;
import cn.web.management.domain.PersonalInform;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AdminQuery {
    private FindAllDao findAllDao = new FindAllImpl();

    /**
     * @description: 查询所有员工信息
     * @paramName: [page, limit]
     * @paramType: [java.lang.String, java.lang.String]
     * @returnType: java.lang.String
     */
    public String findAllPersonalInform(String page, String limit){
        List<PersonalInform> allPersonalInform = findAllDao.findAllPersonalInform();
        String personalInform = formattingJson(page, limit, allPersonalInform);
        return personalInform;
    }

    /**
     * @description: 按照小组号查询员工信息
     * @paramName: [groupno, page, limit]
     * @paramType: [java.lang.String, java.lang.String, java.lang.String]
     * @returnType: java.lang.String
     */
    public String findGroupPersonalInform(String groupno, String page, String limit){
        List<PersonalInform> groupPersonalInform = findAllDao.findGroupPersonalInform(groupno);
        String personalInform = formattingJson(page, limit, groupPersonalInform);
        return personalInform;
    }

    /**
     * @description: 按照部门号查询员工信息
     * @paramName: [departmentno, page, limit]
     * @paramType: [java.lang.String, java.lang.String, java.lang.String]
     * @returnType: java.lang.String
     */
    public String findDepartmentPersonalInform(String departmentno, String page, String limit){
        List<PersonalInform> departmentPersonalInform = findAllDao.findDepartmentPersonalInform(departmentno);
        String s = formattingJson(page, limit, departmentPersonalInform);
        return s;
    }

    /**
     * @description: 按照员工号查询员工信息
     * @paramName: [employno, page, limit]
     * @paramType: [java.lang.String, java.lang.String, java.lang.String]
     * @returnType: java.lang.String
     */
    public String findPersonalInform(String employno, String page, String limit){
        List<PersonalInform> personalInform1 = findAllDao.findPersonalInform1(employno);
        String s = formattingJson(page, limit, personalInform1);
        return s;
    }

    /**
     * @description: 按照员工号查询员工职位信息
     * @paramName: [employno]
     * @paramType: [java.lang.String]
     * @returnType: java.lang.String
     */
    public String findEmployeeCareerInform(String employno){
        List<PersonalInform> personalInform = findAllDao.findPersonalInform1(employno);
        String groupno = personalInform.get(0).getGroupno();

        List<GroupInform> groupInform = findAllDao.findGroupInform(groupno);
        String departmentno = groupInform.get(0).getDepartmentno();

        List<DepartmentInform> departmentInform = findAllDao.findDepartmentInform(departmentno);

        double personalAvgSalary = findAllDao.findPersonalAvgSalary(employno);
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        String format = decimalFormat.format(personalAvgSalary);

        String s = "{";
        s += "\"employname\": \""+personalInform.get(0).getEname()+"\", ";
        s += "\"department_name\": \""+departmentInform.get(0).getDepartment_name()+"\", ";
        s += "\"group_name\": \""+groupInform.get(0).getGroup_name()+"\", ";
        s += "\"current_position\": \""+personalInform.get(0).getCurrent_position()+"\", ";
        s += "\"avg_salary\": \""+format+"\", ";
        s += "\"working_time\":\""+personalInform.get(0).getWorkingtime()+"\",";
        s += "\"employment_time\": \""+personalInform.get(0).getEmployment_time()+"\"";

        s += "}";
        return s;
    }

    /**
     * @description: 查询所有组号
     * @paramName: []
     * @paramType: []
     * @returnType: java.lang.String
     */
    public String findMobilizeInform(){

        String s = "[";
        List<DepartmentInform> departmentInform = findAllDao.findDepartmentInform();
        int size = findAllDao.findGroupInform().size();
        int count = 0;
        for (int i = 0; i < departmentInform.size(); i++) {
            s += "{\"title\":\"";
            s += "<optgroup label=\\\""+ departmentInform.get(i).getDepartment_name() +"\\\">" + "</optgroup>\"}";

            if (count<size)
                s += ",";

            List<GroupInform> groupInform = findAllDao.findGroupDeportmentInform(departmentInform.get(i).getDepartmentno());

            for (int j = 0; j < groupInform.size(); j++) {
                s += "{\"title\":\"";
                s += "<option value=\\\""+ groupInform.get(j).getGroupno() +"\\\">"+ groupInform.get(j).getGroup_name() +"</option>\"}";
                if (count<size-1)
                    s += ",";
                count++;
            }
        }
        s += "]";
        return s;
    }


    public String findGroupPerson(String groupno){
        List<PersonalInform> groupPersonalInform = findAllDao.findGroupPersonalInform(groupno);
        PersonalInform personalInform;
        String s = "[";

        for (int i = 0; i < groupPersonalInform.size(); i++) {
            personalInform = groupPersonalInform.get(i);
            s += "{\"value\":\"" + personalInform.getEmployno()+"\"," ;
            s += "\"title\":\"";
            s += personalInform.getEmployno() + " - ";
            s += personalInform.getEname() + " - ";
            s += personalInform.getCurrent_position() + "  ";
            s += personalInform.getEmployment_time() + " 任职\",";
            s += "\"disabled\":\"\",\"checked\":\"\"}";
            if (i<groupPersonalInform.size()-1)
                s += ",";
        }
        s += "]";
        return s;
    }

    /**
     * @description: 穿梭框获取值
     * @paramName: [groupno1, groupno2]
     * @paramType: [java.lang.String, java.lang.String]
     * @returnType: java.lang.String
     */
    public String findtest(String groupno1, String groupno2){
        List<PersonalInform> groupPersonalInform1 = findAllDao.findGroupPersonalInform(groupno1);
        List<PersonalInform> groupPersonalInform2 = findAllDao.findGroupPersonalInform(groupno2);
        String s = "[";
        PersonalInform personalInform;

        for (int i = 0; i < groupPersonalInform1.size(); i++) {
            personalInform = groupPersonalInform1.get(i);
            s += "{\"value\":\"" + personalInform.getEmployno() + "\",";
            s += "\"title\":\"";
            s += personalInform.getEmployno() + " - ";
            s += personalInform.getEname() + " - ";
            s += personalInform.getCurrent_position() + "  ";
            s += personalInform.getEmployment_time() + " 任职\",";
            s += "\"disabled\":\"\",\"checked\":\"\"}";
            if (i<groupPersonalInform1.size()-1)
                s += ",";
            else if (groupPersonalInform2.size()!=0)
                s += ",";
        }
        for (int i = 0; i < groupPersonalInform2.size(); i++) {
            personalInform = groupPersonalInform2.get(i);
            s += "{\"value\":\"" + personalInform.getEmployno()+"\"," ;
            s += "\"title\":\"";
            s += personalInform.getEmployno() + " - ";
            s += personalInform.getEname() + " - ";
            s += personalInform.getCurrent_position() + "  ";
            s += personalInform.getEmployment_time() + " 任职\",";
            s += "\"disabled\":\"\",\"checked\":\"\"}";
            if (i<groupPersonalInform2.size()-1)
                s += ",";
        }
        s += "]";
        return s;
    }

    /**
     * @description: 获取穿梭框右边的值，数组 value[]
     * @paramName: [groupno2]
     * @paramType: [java.lang.String]
     * @returnType: java.lang.String
     */
    public String rightDate(String groupno2){
        List<PersonalInform> groupPersonalInform = findAllDao.findGroupPersonalInform(groupno2);
        String s = "[";
        for (int i = 0; i < groupPersonalInform.size(); i++) {
            s += "\"" + groupPersonalInform.get(i).getEmployno() + "\"";
            if (i<groupPersonalInform.size()-1)
                s += ",";
        }
        s += "]";
        return s;
    }

    /**
     * @description: 查询所有员工号和姓名，录入工资时下拉栏使用
     * @paramName: []
     * @paramType: []
     * @returnType: java.lang.String
     */
    public String findAllPersonEmployno(){
        List<PersonalInform> allPersonalInform = findAllDao.findAllPersonalInform();
        String s = "[";
        for (int i = 0; i < allPersonalInform.size(); i++) {
            s += "{\"employno\":\"" + allPersonalInform.get(i).getEmployno() + "\",";
            s += "\"ename\":\"" + allPersonalInform.get(i).getEname() + "\"}";
            if (i<allPersonalInform.size()-1)
                s += ",";
        }
        s += "]";
        return  s;
    }

    /**
     * @description: 格式化获取的数据为layui-table所需要的数据接口数据格式，并且实现分页效果
     * @paramName: [page, limit, personalInforms]
     * @paramType: [java.lang.String, java.lang.String, java.util.List<cn.web.management.domain.PersonalInform>]
     * @returnType: java.lang.String
     */
    public static String formattingJson(String page, String limit, List<PersonalInform> personalInforms){
        int page1 = Integer.valueOf(page);
        int limit1 = Integer.valueOf(limit);
        int min = (page1-1)*limit1;
        int max = page1*limit1;

        if (max>personalInforms.size())
            max = personalInforms.size();
        String s = "{\"code\": 0,\"msg\":\"\",\"count\":"+personalInforms.size()+",\"data\":[";
        personalInforms = personalInforms.subList(min, max);
        for (int i = 0; i < personalInforms.size(); i++) {
            PersonalInform personalInform = personalInforms.get(i);
            s+="{\"employno\":\""+personalInform.getEmployno()+"\","+
                    "\"ename\":\""+personalInform.getEname()+"\","+
                    "\"groupno\":\""+personalInform.getGroupno()+"\","+
                    "\"gender\":\""+personalInform.getGender()+"\","+
                    "\"birthday\":\""+personalInform.getBirthday()+"\","+
                    "\"native_place\":\""+personalInform.getNative_place()+"\","+
                    "\"national\":\""+personalInform.getNational()+"\","+
                    "\"political\":\""+personalInform.getPolitical()+"\","+
                    "\"health\":\""+personalInform.getHealth()+"\","+
                    "\"id_card\":\""+personalInform.getId_card()+"\","+
                    "\"tel\":\""+personalInform.getTel()+"\","+
                    "\"school_major\":\""+personalInform.getSchool_major()+"\","+
                    "\"degree\":\""+personalInform.getDegree()+"\"" +
                    "}";
            if(i<personalInforms.size()-1)
                s += ",";
        }
        s += "]}";
        return s;
    }
}
